package com.exercises;


import java.util.Stack;

class Exercise80 {
    public static void main(String[] args) {

        int[] arr = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3};
        int len = removeDuplicates(arr);

        for (int i = 0; i < len; i++) {
            System.out.println(arr[i]);
        }


    }


    public static int removeDuplicates(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (stack.size() > 0 && stack.peek() != nums[i]) {
                stack.clear();
            }
            stack.push(nums[i]);

            if (stack.size() <= 2) {
                nums[index++] = nums[i];
            } else {
                continue;
            }
        }
        return index;

    }


}

